#FEATURE         += DEBUG
CROSS_COMPILE   = 
CC              = $(CROSS_COMPILE)gcc
LD              = $(CROSS_COMPILE)gcc
STRIP           = $(CROSS_COMPILE)strip
SED             = sed

TARGET          = iss
SRCDIRS         = . cJSON udns
EXCLUDE_FILES   = 
INCLUDES        = 
LIBS            = 
STATICLIBS      = 

VERSION         = 

CFLAGS          += -DHAVE_INET_PTON_NTOP -DHAVE_IPv6 -I/usr/local/opt/openssl/include
LDFLAGS         += -lm -lcrypto -lev
DFLAGS          += -MM


ifeq ($(findstring DEBUG, $(FEATURE)),)
  CFLAGS        += -DNDEBUG -O3
else
  CFLAGS        += -Wno-unused-parameter -DDBG -pg -g
  LDFLAGS       += -rdynamic -pg -g
endif

ifneq ($(strip $(STAGING_DIR)),)
  ifneq ($(findstring uClibc, $(CLIB)),)
    CFLAGS      := $(subst -pg, ,$(CFLAGS)) -DNOT_IMPLEMENT_BACKTRACE
    LDFLAGS     := $(subst -pg, ,$(LDFLAGS))
  endif
  INCLUDES      += -I$(STAGING_DIR)/target-$(PLATFORM)_$(CLIB)/usr/include
  LIBS          += -L$(STAGING_DIR)/target-$(PLATFORM)_$(CLIB)/usr/lib
endif

ifeq ($(CROSS_COMPILE),)
  CFLAGS        += -march=native
endif

CFLAGS          := $(CFLAGS) $(addprefix -D, $(FEATURE)) \
                   $(INCLUDES) $(addprefix -I, $(SRCDIRS)) -DVERSION=\"$(VERSION)\" \
                   -pipe -Wall -Wextra
LDFLAGS         := $(STATICLIBS) $(LIBS) $(LDFLAGS)

SRCS            = $(filter-out $(EXCLUDE_FILES), $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c)))
OBJS            = $(SRCS:.c=.o)
DEPENDS         = $(SRCS:.c=.d)

all: $(TARGET)
ifneq ($(findstring -DNDEBUG, $(CFLAGS)),)
	@echo STRIP $(TARGET)
	@$(STRIP) $(TARGET)
endif

$(TARGET): $(OBJS)
	@echo LD $@
	@$(LD) -o $@ $(filter %.o ,$+) $(LDFLAGS)

%.o : %.c
	@echo CC $@
	@$(CC) -c $(CFLAGS) $< -o $@

%.d: %.c
	@echo GEN DEPENDENT $@
	@$(CC) $(DFLAGS) $(CFLAGS) $< | $(SED) 's,$(basename $(notdir $<))\.o\s*:, $(basename $<).o: ,g' > $@

sinclude $(DEPENDS)

clean:
	$(RM) $(OBJS) $(DEPENDS)

distclean: clean
	$(RM) $(TARGET)
